package com.yazi.mxz.gather.main.goods;

import java.util.List;

import org.springframework.data.domain.PageRequest;

import com.myframework.smhj.common.tool.DateUtil;
import com.yazi.mxz.gather.common.App;
import com.yazi.mxz.gather.entity.beans.StoreGoods;
import com.yazi.mxz.gather.entity.repository.Product;
import com.yazi.mxz.gather.repository.ProductRepository;
import com.yazi.mxz.gather.service.StoreGoodsService;
import com.yazi.mxz.gather.service.StoreService;

/**
 * 上传商品
 * @author mo_yq5
 * @date 2015-8-17
 */
public class StoreGoodsUpload extends Thread {
	
	private ProductRepository productRepository;
	private StoreGoodsService storeGoodsService;
	private StoreService storeService;
	
	
	public StoreGoodsUpload() {
		productRepository = App.context.getBean(ProductRepository.class);
		storeGoodsService = App.context.getBean(StoreGoodsService.class);
		storeService = App.context.getBean(StoreService.class);
	}

	@Override
	public void run() {
		com.yazi.mxz.gather.entity.beans.Store store  = new com.yazi.mxz.gather.entity.beans.Store();
		store.setCompanyId(1l);
		List<com.yazi.mxz.gather.entity.beans.Store> list = storeService.list(store);
		List<Product> proList;
		StoreGoods sg;
		Float price;
		String pic;
		for (com.yazi.mxz.gather.entity.beans.Store storeOrc : list) {
			System.out.println("开始添加: " + storeOrc.getName());
			proList = productRepository.findByPicIsNotNull(new PageRequest(new Double(Math.random() * 365).intValue(), 100));
			for (Product pro : proList) {
				if (null != pro.getPrice()) {
					if (pro.getPrice().indexOf("￥") > -1) {
						pro.setPrice(pro.getPrice().replaceAll("￥", "").trim());
						productRepository.save(pro);
					}
					price = Float.parseFloat(pro.getPrice());
				} else {
					price = 0f;
				}
				
				if (null != pro.getMidPic()) {
					pic = pro.getMidPic();
				} else {
					pic = pro.getPic();
				}
				
				sg = new StoreGoods();
				sg.setAddTime(DateUtil.getTimeStamp());
				sg.setAtDiscount(Math.random() > 0.5 ? true : false);
				sg.setAtDiscountLocal(Math.random() > 0.5 ? true : false);
				sg.setAtGift(Math.random() > 0.5 ? true : false);
				sg.setAtGiftLocal(Math.random() > 0.5 ? true : false);
				sg.setBarCode(pro.getBarcode());
				sg.setCategoryPath(pro.getCatName());
				sg.setCatId(117l);// 生活用品
				sg.setClassifyId(2l);// 默认
				sg.setDealAll(new Double(Math.random() * 99999).intValue());
				sg.setDealMonth(new Double(Math.random() * 99999).intValue());
				sg.setDealTime(DateUtil.getTimeStamp());
				sg.setDealWeek(new Double(Math.random() * 99999).intValue());
				sg.setDescription("无介绍");
				sg.setDispatch("无");
				sg.setGoodsId(229l);// 
				sg.setKeywords(pro.getBrandName() + " " + pro.getSpec());
				sg.setName(pro.getName());
				sg.setOriginalPrice(price + price * 0.01f);
				sg.setPic("/sys/" + pic);
				sg.setPrice(price);
				sg.setSelfCost(price);
				sg.setSellDef(1f);
				sg.setSellExt(1f);
				sg.setSellMin(1f);
				sg.setSellUnit(null);
				sg.setSort(new Double(Math.random() * 1439814808103L).longValue());
				sg.setStatus(1);
				sg.setStock((float)(new Double(Math.random()*99).intValue()));
				sg.setStockConvert(1f);
				sg.setStockEnd(0);
				sg.setStore(storeOrc);
				sg.setStoreGoodsId(null);
				sg.setSubTitle(null);
				sg.setTitle(pro.getName());
				sg.setUnit(null != pro.getUnit()?pro.getUnit():"件");
				sg.setUnitConvert(1d);
				sg.setUpdateTime(DateUtil.getTimeStamp());
				try {
					storeGoodsService.persist(sg);
				} catch (Exception e) {
					System.out.println(e.getMessage());
				}
				System.out.println("添加商品: " + pro.getName());
			}
			
			
			storeOrc.setLat((Math.random()*2332 + 2213)/100);
			storeOrc.setLng((Math.random()*10851 + 10745)/100);
			storeOrc.setUpdateTime(DateUtil.getTimeStamp());
			storeService.merge(storeOrc);
			System.out.println("添加结束: " + storeOrc.getName());
			
		}
		
	}
	
}
